package settingsapp.gestures;

import java.io.Serializable;

import android.util.Log;


public class TargetGesture implements Serializable{

	public static final String TAG = TargetGesture.class.getSimpleName();
	public static final int EOG = -2; // End of gesture
	public static final int EMPTY = -1;
	public static final int ACTIVITY_CLOSE_NODE = 3;
	
	private static final long serialVersionUID = 3L; // Last change: added support for performing actions
	private static final int gestureLength = 4;
	
	private int[] gesture;
	private int actionType;
	private int actionId;
	
	
	// Default constructor
	public TargetGesture()
	{		
		gesture = new int[gestureLength];
		for(int i=0; i<gesture.length; i++)
			gesture[i] = EMPTY;
		actionType = -1;
		actionId = -1;
	}
	
	public TargetGesture(int[] gest, int actionType, int actionID)
	{
		gesture = new int[gestureLength];
		
		for(int i=0; i<gestureLength; i++)
		{
			if(i<gest.length)
				gesture[i] = gest[i];
			else
				gesture[i] = EMPTY;			
		}
		
		this.actionType = actionType;
		this.actionId = actionID;
	}
	// Copy constructor
	public TargetGesture(TargetGesture orig, int actionType, int actionID)
	{
		this(orig.gesture, actionType, actionID);		
	}
	
	// Adds the touch of a target to the gesture (order specific)
	public void addTargetTouch(int target)
	{		
		for(int i=0; i<gesture.length; i++)
		{
			// If the target has already been hit, disregard further touches
			if(gesture[i]==target)
				return;
			// If we've reached a free spot in the recording, stop here
			if(gesture[i]==EMPTY)
			{
				gesture[i]=target;
				break;
			}				
		}		
	}
	
	// Empty the gesture
	public void clear()
	{
		for(int i=0; i<gesture.length; i++)
		{
			gesture[i] = EMPTY;
		}
	}
	
	// End the gesture, adding an EOG flag if the gesture is not full
	public void endGesture()
	{
		// Tack on a flag to show the premature ending
		for(int i=0; i<gesture.length; i++)
		{
			if(gesture[i]==EMPTY)
				gesture[i]=EOG;
		}
	}
	
	public boolean equals(TargetGesture other)
	{
		for(int i=0; i<gesture.length; i++)
		{
			if(gesture[i]!=other.gesture[i])
				return false;			
		}
		return true;		
	}
	
	public boolean isActivityClosingGesture()
	{
		for(int i=gestureLength-1; i>=0; i--)
		{
			// Skip past empty or EOG markers
			if(gesture[i] < 0)
				continue;
			
			if(gesture[i] == ACTIVITY_CLOSE_NODE)
				return true;
			return false;
		}
		return false;
	}
		
	public String toString()	
	{
		String result = "";
		
		for(int i=0; i<gesture.length; i++)
		{
			result = result+"->"+gesture[i];
		}
		return result;
	}
	
	public int getActionId()
	{
		return actionId;
	}
	
	public int getActionType()
	{
		return actionType;
	}
	
	// Asymmetric!
	public boolean equalsWithTerminating(TargetGesture other)
	{
		for(int i=0; i<gesture.length; i++)
		{			
			if(gesture[i]!=other.gesture[i])
			{	// If the first gesture is complete, but the second has only a close node left, return true
				if(gesture[i]==EOG && other.gesture[i]==ACTIVITY_CLOSE_NODE)
				{					
					// Make sure that the second gesture completes after the close node
					if(i==3 || other.gesture[i+1]==EOG)
					{						
						return true;						
					}
				}					
				return false;			
			}
		}
		return false;
	}
	
}
